import { Controller, Get, Post, Body, Param, Query, UseGuards, Request } from '@nestjs/common';
import { YijingService, Hexagram } from './yijing.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

export interface DivinationRequest {
  question?: string;
  method?: 'coins' | 'number' | 'random';
}

export interface DivinationResponse {
  hexagram: Hexagram;
  interpretation: string;
  timestamp: string;
}

@Controller('yijing')
export class YijingController {
  constructor(
    private readonly yijingService: YijingService,
  ) { }

  @Get('hexagrams')
  getAllHexagrams(): Hexagram[] {
    return this.yijingService.getAllHexagrams();
  }

  @Get('hexagrams/:id')
  getHexagramById(@Param('id') id: string): Hexagram | undefined {
    const hexagramId = parseInt(id, 10);
    return this.yijingService.getHexagramById(hexagramId);
  }

  @Post('divination')
  async performDivination(@Body() request: DivinationRequest): Promise<DivinationResponse> {
    let hexagram: Hexagram;

    switch (request.method) {
      case 'coins':
        hexagram = this.yijingService.generateHexagramByCoins();
        break;
      case 'number':
        hexagram = this.yijingService.generateHexagramByNumber();
        break;
      default:
        hexagram = this.yijingService.generateHexagram();
    }

    const interpretation = await this.yijingService.interpretHexagram(hexagram, request.question);

    return {
      hexagram,
      interpretation,
      timestamp: new Date().toISOString()
    };
  }

  @Get('divination')
  async performDivinationGet(@Query('question') question?: string, @Query('method') method?: string): Promise<DivinationResponse> {
    const request: DivinationRequest = {
      question,
      method: method as 'coins' | 'number' | 'random'
    };

    return await this.performDivination(request);
  }
}
